<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/15
 * Time: 10:48
 */

namespace app\api\model;

use \Exception;
use think\facade\Db;
use \think\response\Json;

class CartModel
{
    private static $_table = 'cart'; // 购物车表
    private static $goods_specific = 'goods_specific'; // 商品属性表
    private static $goods = 'goods'; // 商品表

    /**购物车列表
     * @param array $input
     * @return Json
     */
    public static function CartList($input)
    {
        $field = 'a.id as cart_id,a.num,b.coop_id,b.center_id,b.id as goods_id,b.info as goods_info,b.name as goods_name,b.image,c.name,c.price,c.center_stock,c.status';
        $data = Db::table(self::$_table)->alias('a')
            ->leftJoin(self::$goods_specific . ' c', 'a.goods_specific_id = c.id')
            ->leftJoin(self::$goods . ' b', 'a.goods_id = b.id')
            ->where('a.status', 1)
            ->where('a.uid', $input['uid'])
            ->field($field)
            ->select()->toArray();
        return jsonSuccess('获取成功', $data);
    }

    /**加入购物车
     * @param array $input
     * @return Json
     */
    public static function AddCart($input)
    {
        $res = Db::table(self::$_table)->where('uid',$input['uid'])->where('status',1)->where('goods_id',$input['goods_id'])->where('goods_specific_id',$input['goods_specific_id'])->count('id');
        try {
            if($res!=0){
                $status = Db::table(self::$_table)->where('uid',$input['uid'])->where('goods_id',$input['goods_id'])->where('goods_specific_id',$input['goods_specific_id'])->inc('num',$input['num'])->update();
                if($status !=0) return jsonSuccess('添加成功');
            }else{
                $input['create_time'] = time();
                $res = Db::table(self::$_table)->insertGetId($input);
                if ($res != 0) return jsonSuccess('添加成功');
            }
        } catch (\Exception $e) {
            return jsonFail($e->getMessage());
        }
    }

    /**删除购物车
     * @param array $input
     * @return Json
     */
    public static function DelCart($input)
    {
        try {
            $res = Db::table(self::$_table)->where('uid', $input['uid'])->where('id', $input['id'])->update(['status' => 2]);
            if ($res != 0) return jsonSuccess('删除成功');
            return jsonFail('删除失败');
        } catch (\Exception $e) {
            return jsonFail($e->getMessage());
        }
    }

    /**删除购物车
     * @param array $input
     * @return Json
     */
    public static function NumCart($input)
    {
        try {
            if($input['type']==1){
                $res = Db::table(self::$_table)->where('id', $input['id'])->where('uid', $input['uid'])->where('goods_id', $input['goods_id'])->inc('num')->update();
            }else if($input['type']==2){
                $res = Db::table(self::$_table)->where('id', $input['id'])->where('uid', $input['uid'])->where('num','>',1)->where('goods_id', $input['goods_id'])->dec('num')->update();
            }else{
                return jsonFail('未知类型错误');
            }
            if ($res != 0) return jsonSuccess('修改成功');
            return jsonFail('修改失败');
        } catch (\Exception $e) {
            return jsonFail($e->getMessage());
        }
    }
    /**购物车商品份数
     * @param array $input
     * @return Json
     */
    public static function CartNum($input)
    {
        $data = Db::table(self::$_table)->where('status',1)->where('uid',$input['uid'])->count('id');
        return jsonSuccess('获取成功',$data);
    }
}